package com.tianruan.dc.modules.userinfo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tianruan.dc.common.utils.IdGen;
import com.tianruan.dc.modules.sys.user.entity.db.UserRoleDO;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import com.tianruan.dc.modules.sys.user.utils.UserUtils;
import com.tianruan.dc.modules.userinfo.context.BaseContext;
import com.tianruan.dc.modules.userinfo.entity.User;
import com.tianruan.dc.modules.userinfo.mapper.UserInfoMapper;
import com.tianruan.dc.modules.userinfo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    public void insertUser(User user){
        //插入一条用户信息
        user.setId(IdGen.uuid());
        user.setCreateBy(BaseContext.getCurrentId());
        user.setCreateDate(LocalDateTime.now());
        user.setUpdateBy(BaseContext.getCurrentId());
        user.setUpdateDate(LocalDateTime.now());
        user.setDelFlag("0");
        userInfoMapper.insertUser(user);
        userInfoMapper.insertSysUser(user);

        if(Objects.equals(user.getType(), "0")){
            UserRoleDO userRoleDO = new UserRoleDO();
            userRoleDO.setUserId(user.getUserId());
            userRoleDO.setRoleId("2");//导师
            userInfoMapper.setTeaRole(userRoleDO);
        }
        else if(Objects.equals(user.getType(), "1")){
            UserRoleDO userRoleDO = new UserRoleDO();
            userRoleDO.setUserId(user.getUserId());
            userRoleDO.setRoleId("3");//学生
            userInfoMapper.setStuRole(userRoleDO);
        }


    }


    public User findUser(String id) {
        User user=userInfoMapper.findUser(id);
        return user;
    }

    @Transactional
    public void updateUser(User user) {
        user.setUpdateBy(UserUtils.getUser().getUsername());
        user.setUpdateDate(LocalDateTime.now());
        userInfoMapper.updateUser(user);
    }


    public void delete(UserDTO id) {
        userInfoMapper.setDelFlag(id);
    }

    public Boolean checkUserId(String id) {
        String count=userInfoMapper.check(id);
        if(count==null){
            return false;
        }
        else {
            return true;
        }
    }


    public void save(User user) {
        userInfoMapper.save(user);
    }


    public Boolean checkUserRoleId(String id) {
        String userRoleId=userInfoMapper.checkUserRoleId(id);
        if(userRoleId==null){
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public PageBean<User> pageStu(Integer page, Integer pageSize, String name, String officeId, String major, String enrollmentYear, String email,
                                  String phone) {
        //创建pageBean对象
        PageBean<User> pageBean = new PageBean<>();
        //开启分页查询  和 模糊查询
        PageHelper.startPage(page,pageSize);
        List<User> pageInfo = userInfoMapper.pageStu(name,officeId,major,enrollmentYear,email,phone);
        Page<User> page1 = (Page<User>) pageInfo;
        //把数据填充到pagebean对象中
        pageBean.setTotal((long) page1.size());
        pageBean.setItems(page1.getResult());
        //返回pagebean对象
        return pageBean;
    }

    @Override
    public PageBean<User> pageTea(Integer page, Integer pageSize, String name,String post, String college, String phone, String major,  String email) {
        //创建pageBean对象
        PageBean<User> pageBean = new PageBean<>();
        //开启分页查询  和 模糊查询
        PageHelper.startPage(page,pageSize);
        List<User> pageInfo = userInfoMapper.pageTea(name,post,college,phone,major,email);
        Page<User> page1 = (Page<User>) pageInfo;
        //把数据填充到pagebean对象中
        pageBean.setTotal((long) page1.size());
        pageBean.setItems(page1.getResult());
        //返回pagebean对象
        return pageBean;
    }

    @Override
    public PageBean<User> pageTutor(Integer page, Integer pageSize, String college, String job, String post, String work_exp, String excel_field, String age) {
        return null;
    }


}
